/* vim: set ts=2 et sw=2 cindent fo=qroca: */

package com.globant.google.mendoza;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.globant.google.malbec.SeleniumBuyerRobot;
import com.globant.google.malbec.XmlCartEncoder;

/** Creates instances of SeleniumBuyerRobot.
 */
public final class SeleniumBuyerRobotFactory {

  /** The class logger.  */
  private static Log log = LogFactory.getLog(SeleniumBuyerRobotFactory.class);

  /** The base url.*/
  private String baseUrl;

  /** The encoder. */
  private XmlCartEncoder encoder;

  /** The firefox path. */
  private String firefoxPath;

  /** The selenium host. */
  private String seleniumHost;

  /** The selenium port. */
  private int seleniumPort;

  /** Constructor. Creates an instance of SeleniumBuyerRobotFactory.
   *
   * @param theBaseUrl The base url. Cannot be null.
   *
   * @param theEncoder The encoder. Cannot be null.
   *
   * @param theFirefoxPath The firefox path. Cannot be null.
   *
   * @param theSeleniumHost The selenium host. Cannot be null.
   *
   * @param theSeleniumPort The selenium port. Cannot be negative.
   */
  public SeleniumBuyerRobotFactory(final String theBaseUrl,
      final XmlCartEncoder theEncoder, final String theFirefoxPath,
     final String theSeleniumHost, final int theSeleniumPort) {
    if (theBaseUrl == null) {
      throw new IllegalArgumentException("The base url cannot be null");
    }
    if (theEncoder == null) {
      throw new IllegalArgumentException("The encoder cannot be null");
    }
    if (theFirefoxPath == null) {
      throw new IllegalArgumentException("The firefox path cannot be null");
    }
    if (theSeleniumHost == null) {
      throw new IllegalArgumentException("The selenium host cannot be null");
    }
    if (theSeleniumPort < 0) {
      throw new IllegalArgumentException(
                "The selenium port cannot be negative");
    }
    baseUrl = theBaseUrl;
    encoder = theEncoder;
    firefoxPath = theFirefoxPath;
    seleniumHost = theSeleniumHost;
    seleniumPort = theSeleniumPort;
  }

  /** Builds the checkout url.
   *
   * @param merchantId The merchant id.
   *
   * @return Returns the checkout url.
   */
  private String buildCheckoutUrl(final String merchantId) {
    log.trace("Entering buildCheckoutUrl");
    final StringBuffer url = new StringBuffer(baseUrl);
    if (!url.toString().endsWith("/")) {
      url.append('/');
    }
    url.append("cws/v2/Merchant/");
    url.append(merchantId);
    url.append('/');
    String checkoutUrl = url.toString() + "checkout";
    log.debug("checkout url: " + checkoutUrl);
    log.trace("Leaving buildCheckoutUrl");
    return checkoutUrl;
  }

  /** Creates an instance of SeleniumBuyerRobot.
   *
   * @param merchantId The merchant id. Cannot be null.
   *
   * @param merchantKey The merchant key. Cannot be null.
   *
   * @return Returns an instance of selenium buyer robot. If something bad
   * happens creating the robot, this method will return null.
   */
  public SeleniumBuyerRobot createSeleniumBuyerRobot(final String merchantId,
      final String merchantKey) {
    log.trace("Entering createSeleniumBuyerRobot");
    if (merchantId == null) {
      log.trace("Leaving createSeleniumBuyerRobot");
      throw new IllegalArgumentException("The merchant id cannot be null");
    }
    if (merchantKey == null) {
      log.trace("Leaving createSeleniumBuyerRobot");
      throw new IllegalArgumentException("The merchant key cannot be null");
    }
    String checkoutUrl = buildCheckoutUrl(merchantId);
    SeleniumBuyerRobot result = null;
    try {
      result = new SeleniumBuyerRobot(baseUrl, checkoutUrl, encoder,
        merchantKey, firefoxPath, seleniumHost, seleniumPort);
    } catch (Exception e) {
      result = null;
    }
    log.trace("Leaving createSeleniumBuyerRobot");
    return result;
  }
}
